/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.wxs.controller;

import cn.webrx.wxs.entity.GoodsBrand;
import cn.webrx.wxs.mapper.GoodsBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: wxshop - AdminGoodsBrandController
 * <p>Powered By webrx
 * <p>Created By IntelliJ IDEA On 2021-04-24 10:48:32
 * <p>Description :
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Controller
@RequestMapping("/admin")
public class AdminGoodsBrandController {
    @Autowired
    private GoodsBrandMapper gbdao;

    @GetMapping("goodsBrand/lists")
    public String lists() {
        return "admin/goods_brand/lists";
    }

    @GetMapping("goodsBrand/add")
    public String add() {
        return "admin/goods_brand/add";
    }

    @PostMapping("goodsBrand/asave")
    @ResponseBody
    public Map<String, Object> asave(GoodsBrand brand) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (gbdao.save(brand) == 1) {
            map.put("code", 1);
            map.put("msg", "添加成功");
        } else {
            map.put("code", 0);
            map.put("msg", "添加品牌失败");
        }
        return map;
    }

    @GetMapping("goodsBrand/switch")
    @ResponseBody
    public Map<String, Object> switchs(int id, int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (gbdao.updateStatus(id, status) == 1) {
            map.put("code", 1);
            map.put("msg", "设置成功");
        } else {
            map.put("code", 0);
            map.put("msg", "设置失败");
        }
        return map;
    }

    //
    @PostMapping("goodsBrand/del/{id}")
    @ResponseBody
    public Map<String, Object> del(@PathVariable int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (gbdao.deleteByPrimaryKey(id) == 1) {
            map.put("code", 1);
            map.put("msg", "删除成功");
        } else {
            map.put("code", 0);
            map.put("msg", "删除失败");
        }

        return map;
    }

    @GetMapping("goodsBrand/edit/{id}")
    public ModelAndView edit(@PathVariable int id) {
        ModelAndView mv = new ModelAndView("admin/goods_brand/edit");
        mv.addObject("brand", gbdao.selectByPrimaryKey(id));
        return mv;
    }

    @PostMapping("goodsBrand/esave")
    @ResponseBody
    public Map<String, Object> esave(GoodsBrand brand) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (gbdao.update(brand) == 1) {
            map.put("code", 1);
            map.put("msg", "设置成功");
        } else {
            map.put("code", 0);
            map.put("msg", "设置失败");
        }
        return map;
    }


    @GetMapping("goodsBrand/index")
    @ResponseBody
    public Map index(int page, int limit, @RequestParam(name = "name", required = false) String name) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 1);
        map.put("msg", "");
        //查分分页时，page 第几页 limit 是页多少条,sort desc 使用排序 order by sort desc
        PageHelper.startPage(page, limit, "sort desc");
        PageInfo p = PageInfo.of(gbdao.page(name == null ? null : "%" + name + "%"));
        map.put("count", p.getTotal());
        map.put("data", p.getList());
        return map;
    }
}
